package com.jsh.erp.base;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class DeepSeekClient {
    private final CloseableHttpClient httpClient;
    private final String apiUrl;
    private final String apiKey;

    public DeepSeekClient(CloseableHttpClient httpClient, String apiUrl, String apiKey) {
        this.httpClient = httpClient;
        this.apiUrl = apiUrl;
        this.apiKey = apiKey;
    }

    public String chatCompletion(String prompt) throws IOException {
        HttpPost request = new HttpPost(apiUrl + "/v1/chat/completions");

        // 构建请求体
        String requestBody = String.format(
                "{\"model\":\"deepseek-chat\",\"messages\":[{\"role\":\"user\",\"content\":\"%s\"}]}",
                prompt.replace("\"", "\\\"")
        );

        // 设置请求头
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Authorization", "Bearer " + apiKey);
        request.setEntity(new StringEntity(requestBody));

        // 发送请求
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity());
        }
    }
}
